Public Class Form1
Dim hComm As Integer 'COMポート用ハンドル
Dim stDCB As DCB 'DCBインスタンス
Dim timeOut As COMMTIMEOUTS 'タイムアウト用インスタンス
Dim wDATA As String 'COM送信バッファ
Dim rDATA(100) As Byte 'COM受信バッファ
Dim dLen, wLen, rLen As Int32 'COM用パラメータ
Dim bRet As Boolean '関数戻り値
Dim CommName As String 'COMポート番号
Dim Flag As Integer
Dim TB0, TB1 As String
Dim OutData, OldData As Long
Dim Index, i As Integer
Dim DG(3000), DT(3000) As Integer
Dim TBoxG(3000), TBoxT(3000) As Integer
Dim DataG(3000), DataT(3000) As Integer
Dim Xd, Yd1, Xdif, Ydif1, Yd2, Ydif2 As Single
Dim W As Integer = Width
Dim H As Integer = Height
Private Sub TimeOutSet() 'COM用タイムアウト関数
timeOut.ReadIntervalTimeout = 20
timeOut.ReadTotalTimeoutConstant = 50
timeOut.ReadTotalTimeoutMultiplier = 10
timeOut.WriteTotalTimeoutMultiplier = 10
timeOut.WriteTotalTimeoutConstant = 10
bRet = SetCommTimeouts(hComm, timeOut)
End Sub
'USB接続ボタン
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
CommName = TextBox1.Text 'COMポート番号取得
bRet = CloseHandle(hComm)
hComm = CreateFile(CommName, &HC0000000, 0, IntPtr.Zero, &H3, &H80, IntPtr.Zero)
If hComm <> -1 Then
stDCB.BaudRate = 115200 'DCB構造体データ設定
stDCB.fBitFields = &H1
stDCB.ByteSize = 8
stDCB.Parity = 0
stDCB.StopBits = 0
bRet = SetCommState(hComm, stDCB)
TimeOutSet()
If bRet = True Then '接続確認 コマンド「0」の応答「OK」をチェック
Application.DoEvents()
wDATA = Chr(&H30) 'コマンド0 (&H30 = 0)
dLen = 1
bRet = WriteFile(hComm, wDATA, dLen, wLen, IntPtr.Zero)
bRet = ReadFile(hComm, rDATA, 30, rLen, IntPtr.Zero)
If bRet = True And rLen = 3 Then
If rDATA(0) = &H4F And rDATA(1) = &H4B Then ' OKが返信
TextBox2.Text = "Connect" '正常接続完了
Flag = 0
Else
TextBox2.Text = "False" '接続異常
End If
Else
TextBox2.Text = "NoAns" '応答なし
End If
Else
TextBox2.Text = "Fault" '接続不可
End If
Else
TextBox2.Text = "NoExist" 'デバイス見つからず
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Timer1.Interval = 10500 'タイマー時間設定(10秒+α)
Timer1.Enabled = True 'タイマー スタート
wDATA = Chr(&H31) 'コマンド0x31 (=1) 送信
dLen = Len(wDATA)
bRet = WriteFile(hComm, wDATA, dLen, wLen, IntPtr.Zero) 'コマンド送信後
OutData = 1
OldData = 1
Index = -1
DG(0) = 0
DT(0) = 0
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Timer1.Enabled = False 'タイマー 一時停止
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Me.Refresh()
Index = -1
TextBox3.Text = ""
TextBox4.Text = ""
End Sub
'一定時間ごとの計測と描画
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim N As Integer
Dim G1 As Graphics = PictureBox1.CreateGraphics 'PictureBoxごとにグラフィックを宣言
Dim G2 As Graphics = PictureBox2.CreateGraphics
Dim p1 As New Pen(Color.Red)
p1.Width = 2
Dim p2 As New Pen(Color.HotPink)
p2.Width = 2
G1.ScaleTransform(1, -1) 'Y座標の極性反転
Xd = W / 5 'X,Y座標ステップの設定
Yd1 = H / 10.9 '(* 出来合いの罫線に対する調整箇所@: 10.9)
Xdif = Xd / 60
Ydif1 = Yd1 / 10
G1.TranslateTransform(0, -215) '原点座標の設定
G2.ScaleTransform(1, -1) 'Y座標の極性反転
Yd2 = H / 25
Ydif2 = Yd2 / 10
G2.TranslateTransform(0, -117) '原点座標の設定
Index = Index + 1
wDATA = Chr(&H31) 'コマンド0x31 (=1) 送信
dLen = Len(wDATA)
bRet = WriteFile(hComm, wDATA, dLen, wLen, IntPtr.Zero) 'コマンド送信
bRet = ReadFile(hComm, rDATA, 64, rLen, IntPtr.Zero) 'データ受信(前回分)
If bRet = True Then
TB0 = ""
TB1 = ""
TextBox3.Text = ""
TextBox4.Text = ""
For N = 0 To 4 Step 1
TB0 = TB0 & Chr(rDATA(N))
Next
For N = 7 To 10 Step 1
TB1 = TB1 & Chr(rDATA(N))
Next
' Data0(Index) = Val(TB0 & Chr(0))
' Data1(Index) = Val(TB1 & Chr(0))
TextBox3.Text = TextBox3.Text & TB0 & Chr(&H20)
TextBox4.Text = TextBox4.Text & TB1 & Chr(&H20)
DG(Index) = Val(TextBox3.Text & Chr(0)) 'Val: 文字列を数値化
DT(Index) = Val(TextBox4.Text & Chr(0))
End If
If Index > 0 Then
If DG(Index) = 0 Then 'LOG 0 = -∞ オーバーフロー・エラー対策
DG(Index) = 3
End If
If DG(Index - 1) = 0 Then
DG(Index - 1) = 3
End If
'折れ線グラフの描画'(* 出来合いの罫線に対する調整箇所A: - 11、- 4)
G1.DrawLine(p1, OldData, Conv(DG(Index - 1)) * Ydif1 - 11, OutData, Conv(DG(Index)) * Ydif1 - 11)
G2.DrawLine(p2, OldData, DT(Index - 1) * Ydif2 - 4, OutData, DT(Index) * Ydif2 - 4)
End If
OldData = OutData
OutData = OutData + Xdif
TBoxG(Index) = DG(Index)
TBoxT(Index) = DT(Index)
End Sub
Private Function Conv(ByVal data) As Integer
Return (CType((Math.Log10(data) - 1) * Yd1, Long))
End Function
'罫線の描画 PictureBox1
Private Sub GaiarChart(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim Xd, Yd As Integer
Dim i, j As Integer
e.Graphics.ScaleTransform(1, -1) 'Y座標の極性反転
Xd = W / 5 '座標ステップ
Yd = H / 4
e.Graphics.TranslateTransform(1, -2 * Yd) '原点座標の設定
e.Graphics.Clear(Color.White) '背景色 白クリア
'横軸 LOG表示
For i = -2 To 2
For j = 1 To 9
e.Graphics.DrawLine(Pens.Gray, W * 2, CType(Math.Log10(j * 10 ^ i) * Yd, Integer), 0, CType(Math.Log10(j * 10 ^ i) * Yd, Integer))
Next
Next
e.Graphics.DrawLine(Pens.BlueViolet, 1, 0, W * 2, 0)
'縦軸
For i = 0 To 10
e.Graphics.DrawLine(Pens.Gray, -(i - 10) * Xd, H * 2, -(i - 10) * Xd, -H * 2)
Next
e.Graphics.DrawLine(Pens.BlueViolet, 1, H * 2, 1, -H * 2)
End Sub
'罫線の描画 PictureBox2
Private Sub TempChart(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox2.Paint
Dim Xd, Yd As Integer
Dim i As Integer
e.Graphics.ScaleTransform(1, -1) 'Y座標の極性反転
Xd = W / 5 'X,Y座標ステップの設定
Yd = H / 25
e.Graphics.TranslateTransform(1, -1) '原点座標の設定
e.Graphics.Clear(Color.White) '背景色 白クリア
'横軸
For i = 0 To 10
e.Graphics.DrawLine(Pens.Gray, 1, (i - 9) * Yd, W * 2, (i - 9) * Yd)
Next
e.Graphics.DrawLine(Pens.BlueViolet, 1, 0, W * 2, 0)
'縦軸
For i = 0 To 10
e.Graphics.DrawLine(Pens.Gray, -(i - 10) * Xd, 0, -(i - 10) * Xd, -H * 2)
Next
e.Graphics.DrawLine(Pens.BlueViolet, 1, 0, 1, -H * 2)
End Sub
'データの保存
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim myFile As String = ""
Dim i As Integer
Dim tw As System.IO.TextWriter
SaveFileDialog1.Filter = "csvファイル(*.csv)|*.csv"
If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
myFile = SaveFileDialog1.FileName
End If
If myFile <> "" Then
Try
tw = My.Computer.FileSystem.OpenTextFileWriter(myFile, False, System.Text.Encoding.Default)
For i = 0 To Index - 1
tw.WriteLine(Str(TBoxG(i))) 'Str: 数値を文字列化、G、T 交互に記録
tw.WriteLine(Str(TBoxT(i)))
Next
tw.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
'読み出しと再描画
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim myFile As String = ""
Dim tr As System.IO.TextReader
Dim Item, i As Integer
Dim ss As String
Dim G1 As Graphics = PictureBox1.CreateGraphics
Dim G2 As Graphics = PictureBox2.CreateGraphics
Dim p1 As New Pen(Color.Red)
p1.Width = 2
Dim p2 As New Pen(Color.HotPink)
p2.Width = 2
G1.ScaleTransform(1, -1) 'Y座標の極性反転
Xd = W / 5 'X,Y座標ステップの設定
Yd1 = H / 10.9
Xdif = Xd / 60
Ydif1 = Yd1 / 10
G1.TranslateTransform(0, -215) '原点座標の設定
G2.ScaleTransform(1, -1) 'Y座標の極性反転
Yd2 = H / 25
Ydif2 = Yd2 / 10
G2.TranslateTransform(0, -117) '原点座標の設定
OpenFileDialog1.Filter = "csvファイル(*.csv)|*.csv"
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
myFile = OpenFileDialog1.FileName
Try
tr = My.Computer.FileSystem.OpenTextFileReader(myFile, System.Text.Encoding.Default)
Item = 0
Do
ss = tr.ReadLine()
DataG(Item) = Val(ss)
ss = tr.ReadLine()
DataT(Item) = Val(ss)
Item = Item + 1
Loop While ss IsNot Nothing
tr.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
OutData = 1
OldData = 1
For i = 0 To Item - 1 'LOG 0 = -∞ オーバーフロー・エラー対策
If DataG(i) = 0 Then
DataG(i) = 3
End If
If DataG(i + 1) = 0 Then
DataG(i + 1) = 3
End If
'折れ線グラフの再描画'
G1.DrawLine(p1, OldData, Conv(DataG(i)) * Ydif1 - 11, OutData, Conv(DataG(i + 1)) * Ydif1 - 11)
G2.DrawLine(p2, OldData, DataT(i) * Ydif2 - 4, OutData, DataT(i + 1) * Ydif2 - 4)
OldData = OutData
OutData = OutData + Xdif
Next
End If
End Sub
End Class
(モジュール1)
Module Module1
Public Structure DCB
'DCB構造体宣言
Public DCBlength As Int32
Public BaudRate As Int32
Public fBitFields As Int32
Public wReserved As Int16
Public XonLim As Int16
Public XoffLim As Int16
Public ByteSize As Byte
Public Parity As Byte
Public StopBits As Byte
Public XonChar As Byte
Public XoffChar As Byte
Public ErrorChar As Byte
Public EofChar As Byte
Public EvtChar As Byte
End Structure
Public Structure COMMTIMEOUTS
'タイムアウト用構造体宣言
Public ReadIntervalTimeout As Int32
Public ReadTotalTimeoutMultiplier As Int32
Public ReadTotalTimeoutConstant As Int32
Public WriteTotalTimeoutMultiplier As Int32
Public WriteTotalTimeoutConstant As Int32
End Structure
'COMポート用関数宣言
Public Declare Function CreateFile Lib "kernel32.dll" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Int32, ByVal dwShareMode As Int32, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreatonDisposition As Int32, ByVal dwFlagsAndAttributes As Int32, ByVal hTemplateFile As IntPtr) As Integer
Public Declare Function CloseHandle Lib "kernel32.dll" (ByVal hComm As Integer) As Boolean
Public Declare Function SetCommState Lib "kernel32.dll" (ByVal hComm As Integer, ByRef lpDCB As DCB) As Boolean
Public Declare Function WriteFile Lib "kernel32.dll" (ByVal hComm As Integer, ByVal lpBuffer As String, ByVal nNumberOfBytesToWrite As Int32, ByRef lpNumberOfBytesWritten As Int32, ByVal lpOverlapped As IntPtr) As Boolean
Public Declare Function ReadFile Lib "kernel32.dll" (ByVal hComm As Integer, ByVal lpBuffer As Byte(), ByVal nNumberOfBytesToRead As Int32, ByRef lpNumberOfBytesRead As Int32, ByVal lpOverlapped As IntPtr) As Boolean
Public Declare Function SetCommTimeouts Lib "kernel32.dll" (ByVal hComm As Integer, ByRef lpCommTimeouts As COMMTIMEOUTS) As Boolean
Public Declare Function GetCommState Lib "kernel32.dll" (ByVal hComm As Integer, ByRef lpDCB As DCB) As Boolean
End Module